package com.ffapp.rainmonitorcustom.util;

import android.text.TextUtils;

import com.ffapp.rainmonitorcustom.basis.BasisApp;
import com.ffapp.rainmonitorcustom.basis.Constants;
import com.ffapp.rainmonitorcustom.bean.LoginServiceListData;

import my.LogUtil;
import my.MySharedPreferences;

/**
 * Created by niany on
 */

public class MyUtils {

    private static final String SP_APPID= "sp_appid";
    private static final String SP_IP= "app_ip";


    public static String getIp(){
        MySharedPreferences msp = new MySharedPreferences(BasisApp.mContext);
        if(TextUtils.isEmpty(msp.getString(SP_IP))){
            return  Constants.URL_SERVICE;
        }

        return msp.getString(SP_IP);
    }

    public static void putIP( String ip){
        MySharedPreferences msp = new MySharedPreferences(BasisApp.getInstance());
        if(!ip.startsWith("http")){
            ip = "http://"+ip;
        }
        msp.putString(SP_IP,ip);
    }

    public static String getServiceAPI(){
        return getIp()+ "";
    }

    public static String getImageUrlBySize(String urlorg,int with,int height){
        if(TextUtils.isEmpty(urlorg))return "";
        if(urlorg.endsWith(".jpg")){
          return   urlorg+"@w"+with+"_h"+height+".jpg";
        }else if(urlorg.endsWith(".png")){
            return   urlorg+"@w"+with+"_h"+height+".png";
        }else{
            return   urlorg+"@w"+with+"_h"+height+".jpg";
        }
    }

    public static String getImageUrlHome(String urlorg){
        if(TextUtils.isEmpty(urlorg))return "";
//        return Constants.URL_SERVICE+urlorg;
        return getIp()+urlorg;
    }
    public static String getImageUrl(String urlorg){
        if(TextUtils.isEmpty(urlorg))return "";

        LogUtil.i("getImageUrl--->"+Constants.URL_API_IMG+urlorg);
        return   getServiceAPI()+Constants.URL_API_IMG+urlorg;
    }


//    public static String getIP( ){
//        MySharedPreferences msp = new MySharedPreferences(BasisApp.getInstance());
//        return  msp.getString(SP_SERVERURL);
//    }

    public static String getAppid( ){
        MySharedPreferences msp = new MySharedPreferences(BasisApp.getInstance());
        return  msp.getString(SP_APPID,"10001");
    }
    public static void putAppid( String appid){
        MySharedPreferences msp = new MySharedPreferences(BasisApp.getInstance());
          msp.putString(SP_APPID,appid);
    }
    public static String getUser( ){
        MySharedPreferences msp = new MySharedPreferences(BasisApp.getInstance());
        return  msp.getUser();
    }
    public static void putUser( String user){
        MySharedPreferences msp = new MySharedPreferences(BasisApp.getInstance());
          msp.putUser(user);
    }

    public static  void putSerciceData(LoginServiceListData data){
        if(data == null)return;
        MySharedPreferences msp = new MySharedPreferences(BasisApp.getInstance());
        msp.putString(SP_IP,data.getIp());
//        msp.putString(SP_PORT,data.getPort());
    }
    public static  LoginServiceListData getSerciceData(){
        MySharedPreferences msp = new MySharedPreferences(BasisApp.getInstance());
        if(TextUtils.isEmpty(msp.getString(SP_IP))){
            return null;
        }
        LoginServiceListData loginServiceListData = new LoginServiceListData();
        loginServiceListData.setIp(msp.getString(SP_IP));
        return  loginServiceListData;
    }

}
